//
//  LTFTopicTopView.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/20.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFTopicTopView.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "LTFBaseModel.h"
#import "LTFTopicTopModel.h"
#import "IsNight.h"
#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height
@interface LTFTopicTopView ()
@property (nonatomic, retain) UIImageView *topImageView;//头部背景
@property (nonatomic, retain) UIView *topClearView;//头部透明背景
@property (nonatomic, retain) UILabel *topLabel;//描述
@property (nonatomic, retain) UILabel *attentionLabel;//关注
@property (nonatomic, retain) UIButton *attentionButton;//关注按钮
@end

@implementation LTFTopicTopView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
        [self createData];
    }
    return self;
}
- (void)setString:(NSString *)string {
    if (_string != string) {
        [_string release];
        _string = [string retain];
        [self createData];
    }
}
- (void)createView {
    self.topImageView =[[UIImageView alloc] init];
    self.topImageView.backgroundColor = [UIColor blueColor];
    [self addSubview:_topImageView];
    [_topImageView release];
    
    
    self.topClearView = [[UIView alloc] init];
    self.topClearView.backgroundColor = [UIColor blackColor];
    self.topClearView.alpha = 0.3;
    [self addSubview:_topClearView];
    [_topClearView release];
    
    
    self.topLabel = [[UILabel alloc] init];
    //self.topLabel.backgroundColor = [UIColor redColor];
    [self addSubview:_topLabel];
    [_topLabel release];
    
    
    self.attentionLabel = [[UILabel alloc] init];
    //self.attentionLabel.backgroundColor = [UIColor blackColor];
    [self addSubview:_attentionLabel];
    [_attentionLabel release];
    
    
    self.attentionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.attentionButton.backgroundColor = [UIColor redColor];
    [self addSubview:_attentionButton];
    if ([IsNight nowIsNight]) {
        _topLabel.textColor = [IsNight textNightColor];
        _attentionLabel.textColor = [IsNight textNightColor];
    }
    else {
        _topLabel.textColor = [UIColor blackColor];
        _attentionLabel.textColor = [UIColor blackColor];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.topImageView.frame = self.frame;
    self.topClearView.frame = self.frame;
    self.topLabel.frame = CGRectMake(30, HEIGHT / 3 - 10, WIDTH - 60, 50);
    self.topLabel.textAlignment = NSTextAlignmentCenter;
    self.topLabel.textColor = [UIColor whiteColor];
    self.attentionLabel.frame = CGRectMake(0, 0, WIDTH / 3 + 10, 30);
    self.attentionLabel.center = CGPointMake(WIDTH / 2, HEIGHT / 3 + 60);
    self.attentionLabel.textColor = [UIColor whiteColor];
//    self.attentionButton.frame = CGRectMake(0, 0, WIDTH / 4, 30);
//    self.attentionButton.center = CGPointMake(WIDTH / 2, HEIGHT / 3 + 100);
//    [self.attentionButton setTitle:@" + 关注 " forState:UIControlStateNormal];
//    self.attentionButton.layer.cornerRadius = 15;
//    self.attentionButton.layer.masksToBounds = YES;
//    self.attentionButton.tintColor = [UIColor whiteColor];
}


- (void)createData {
    NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/qa/%@.html", self.string];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:string parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dictionary = responseObject;
        NSDictionary *data = dictionary[@"data"];
        NSDictionary *expert = data[@"expert"];
        self.topLabel.text = expert[@"alias"];
        self.topLabel.numberOfLines = 0;
        [self.topLabel sizeToFit];
        self.attentionLabel.text = [NSString stringWithFormat:@"-- %.1f万关注 --", [expert[@"concernCount"] intValue] / 10000.0];
        [self.topImageView setImageWithURL:[NSURL URLWithString:expert[@"picurl"]] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];


    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
